package com.example.demo.config;

import com.example.demo.common.AppVariable;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created with IntelliJ IEDA.
 * Description:
 * User:86186
 * Date:2023-08-29
 * Time:14:49
 */

/**
 * 登录拦截器
 */
public class LoginInterceptort implements HandlerInterceptor {
    /**
     * 当返回 true 时表示用户已登陆
     * false 用户未登录
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession(false);
        if (session != null && session.getAttribute("user") != null){
            // 用户已登录
            return true;
        }
        // 用户未登录 跳转到登陆页面
        response.sendRedirect("/login.html");
        return false;
    }
}
